using BackData;
using Domain.生产订单;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Order;

namespace Team.API.Applocation.Hanldler.Order
{
    /// <summary>
    /// 更新订单命令处理器
    /// </summary>
    public class UpdateOrderHandler : IRequestHandler<UpdateOrderCommand, APIResult<int>>
    {
        private readonly IRepository<OrderModel> _orderRep;
        private readonly ILogger<UpdateOrderHandler> _logger;

        public UpdateOrderHandler(
            IRepository<OrderModel> orderRep,
            ILogger<UpdateOrderHandler> logger)
        {
            _orderRep = orderRep;
            _logger = logger;
        }

        public async Task<APIResult<int>> Handle(UpdateOrderCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 查找订单
                var order = await _orderRep.GetAll()
                    .Where(x => x.Id == request.Id && !x.IsDel)
                    .FirstAsync();

                if (order == null)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "订单不存在",
                        Result = 0
                    };
                }

                // 更新字段（只更新非空字段）
                if (request.Itemcode.HasValue)
                {
                    if (request.Itemcode.Value <= 0)
                    {
                        return new APIResult<int>
                        {
                            Code = StateCode.Fail,
                            Message = "成品编号无效",
                            Result = 0
                        };
                    }
                    order.Itemcode = request.Itemcode.Value;
                }

                if (request.Number.HasValue)
                {
                    if (request.Number.Value <= 0)
                    {
                        return new APIResult<int>
                        {
                            Code = StateCode.Fail,
                            Message = "订单数量必须大于0",
                            Result = 0
                        };
                    }
                    order.Number = request.Number.Value;
                }

                if (request.Ordertime.HasValue)
                    order.Ordertime = request.Ordertime.Value;

                if (request.Orderstatus.HasValue)
                    order.Orderstatus = request.Orderstatus.Value;

                var result = await _orderRep.UpdateAsync(order);

                if (result > 0)
                {
                    _logger.LogInformation($"订单更新成功: ID={request.Id}");
                    return new APIResult<int>
                    {
                        Code = StateCode.OK,
                        Message = "订单更新成功",
                        Result = result
                    };
                }
                else
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "订单更新失败",
                        Result = 0
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "订单更新失败");
                return new APIResult<int>
                {
                    Code = StateCode.Fail,
                    Message = $"订单更新失败: {ex.Message}",
                    Result = 0
                };
            }
        }
    }
}
